package com.gongpingjialoan.view;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gongpingjialoan.R;
import com.gongpingjialoan.api.API;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.utility.DhUtil;
import com.gongpingjialoan.utility.StringUtils;
import com.gongpingjialoan.widget.LoadingDialog;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONObject;

/**
 * Created by Administrator on 2017/5/17.
 */
public class GetPhoneDialog implements View.OnClickListener {
    // 关闭
    private ImageView dialog_close;
    private AppCompatDialog mDialog;
    private Context mContext;
    LoadingDialog loadingDialog;
    TextView yes;
    OnPickResultListener onPickResultListener;
    private int padding;
    private EditText check_phone_et;

    private LinearLayout getPhone_ll;

    public GetPhoneDialog(Context context) {
        this.mContext = context;
        padding = DhUtil.dip2px(context, 10);
    }

    public void show(int topMargin) {
        android.support.v7.app.AlertDialog.Builder dlg = new android.support.v7.app.AlertDialog.Builder(mContext, R.style.dialogs);
        View view = View.inflate(mContext, R.layout.getphone_dialog, null);

        // 关闭按钮
        dialog_close = (ImageView) view.findViewById(R.id.dialog_close);
        dialog_close.setOnClickListener(this);
        getPhone_ll = (LinearLayout)view.findViewById(R.id.getPhone_ll);
        LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
        layoutParam.setMargins(0, topMargin, 0, 0);
        getPhone_ll.setLayoutParams(layoutParam);
        yes = (TextView) view.findViewById(R.id.dialog_btn);
        yes.setEnabled(false);
        yes.setOnClickListener(this);
        check_phone_et = (EditText) view.findViewById(R.id.check_phone_et);
        getPhone_ll = (LinearLayout)view.findViewById(R.id.getPhone_ll);
        check_phone_et.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                if (check_phone_et.getText().length() == 11) {
                    yes.setEnabled(true);
                } else {
                    yes.setEnabled(false);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                          int arg3) {
            }

            @Override
            public void afterTextChanged(Editable arg0) {
            }
        });
        dlg.setView(view);
        mDialog = dlg.create();
        mDialog.setCancelable(true);
        mDialog.setCanceledOnTouchOutside(false);
        mDialog.getWindow().getDecorView().setPadding(padding, 0, padding, 0);
        mDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED);
        mDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                    onPickResultListener.onResult(0);
                }
                return false;
            }
        });
        mDialog.show();

    }

    private void dismiss() {
        mDialog.dismiss();
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.dialog_close:
                onPickResultListener.onResult(0);
                break;
            case R.id.dialog_btn:
                yesbutton();
                break;
            default:
                break;
        }
    }

    public void yesbutton() {
        String phone = check_phone_et.getText().toString();
        if(!StringUtils.isChinaPhoneLegal(phone)){
            Toast.makeText(mContext,"请输入正确的手机号",Toast.LENGTH_SHORT).show();
            return;
        }
        loadingDialog = new LoadingDialog(mContext);
        loadingDialog.show();
        NetDataJson mNetDataJson = new NetDataJson(new NetDataJson.OnNetDataJsonListener() {


            @Override
            public void onDataJsonUpdate(JSONObject json) {
                loadingDialog.dismiss();
                dismiss();
                mContext.getSharedPreferences("GetPhone", mContext.MODE_PRIVATE).edit().putBoolean("isShowGetPhoneDialog",false).commit();
                MobclickAgent.onEvent(mContext,"ReckonMobileCommit");
            }

            @Override
            public void onDataJsonError(String errorMessage) {
                Toast.makeText(mContext, errorMessage, Toast.LENGTH_SHORT).show();
            }
        });
        mNetDataJson.setUrl(API.record_phone);
        mNetDataJson.addParam("phone", phone);
        mNetDataJson.addParam("eval_type","buy");
        mNetDataJson.request("post");
    }

    public interface OnPickResultListener {
        void onResult(int result);
    }

    public OnPickResultListener getOnPickResultListener() {
        return onPickResultListener;
    }

    public void setOnPickResultListener(
            OnPickResultListener onPickResultListener) {
        this.onPickResultListener = onPickResultListener;
    }
}

